package com.pinyoumei.app.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.pinyoumei.app.R;
import com.pinyoumei.app.adapter.SignInRecordAdapter;
import com.pinyoumei.app.bean.SignRecordBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2021-08-12.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2021-08-12        Administrator             新增Person类
 * 2021-08-12        Administrator             增加sex属性
 * 签到记录
 */
public class SignRecordActivity extends BaseActivity implements View.OnClickListener {

    private TextView tvSignAll;
    private TextView tvSign;
    private TextView tvGiveAll;
    private int page = 1;
    private SignInRecordAdapter signInRecordAdapter;
    private RecyclerView rv1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_record);

        InitWidget();

        InitData();
    }

    private void InitWidget() {


        ImageView ivClose = findViewById(R.id.iv_close);
        ivClose.setOnClickListener(this);


        tvSignAll = findViewById(R.id.tv_sign_all);
        tvSign = findViewById(R.id.tv_sign);
        tvGiveAll = findViewById(R.id.tv_give_all);


        rv1 = findViewById(R.id.rv_1);
        rv1.setLayoutManager(new GridLayoutManager(this, 1, GridLayoutManager.VERTICAL, false));
        signInRecordAdapter = new SignInRecordAdapter(R.layout.item_sign_record, null);
        signInRecordAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rv1.setAdapter(signInRecordAdapter);

        SmartRefreshLayout swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                LoadData();
                refreshLayout.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                InitData();
                refreshLayout.finishRefresh();
            }
        });
    }

    private void InitData() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(SignRecordActivity.this));
        map.put("page", page);
        map.put("limit", 4);


        OkGo.<String>post(Constants.SIGN_LIST)
                .params("user_id", SPUtils.getAppUserId(SignRecordActivity.this))
                .params("page", page)
                .params("limit", 4)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        setData(response);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        SignRecordBean signRecordBean = gson.fromJson(response.body(), SignRecordBean.class);

                        if (signRecordBean.getCode() >= 0) {
                            tvSign.setText(signRecordBean.getResult().getContinue_sign_day()+"天");
                            tvSignAll.setText(signRecordBean.getResult().getTotal_sign_day()+"天");
                            tvGiveAll.setText(signRecordBean.getResult().getTotal_group_coupon()+"张");

                            signInRecordAdapter.setNewData(signRecordBean.getResult().getSign_list());
                            View notDataView = getLayoutInflater().inflate(R.layout.item_empty_view, (ViewGroup) rv1.getParent(), false);
                            signInRecordAdapter.setEmptyView(notDataView);


                        } else {
                            Toast.makeText(SignRecordActivity.this, signRecordBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }

                    }

                });
    }




    private void LoadData() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(SignRecordActivity.this));
        map.put("page", page);
        map.put("limit", 4);


        OkGo.<String>post(Constants.SIGN_LIST)
                .params("user_id", SPUtils.getAppUserId(SignRecordActivity.this))
                .params("page", page)
                .params("limit", 4)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        setData(response);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        SignRecordBean signRecordBean = gson.fromJson(response.body(), SignRecordBean.class);

                        if (signRecordBean.getCode() >= 0) {

                            signInRecordAdapter.addData(signRecordBean.getResult().getSign_list());
                        } else {
                            Toast.makeText(SignRecordActivity.this, signRecordBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }

                    }

                });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_close:
                finish();
                break;

            default:
                break;
        }
    }
}
